import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd';

declare var form: any;

@Component({
  templateUrl: './portal.document.info.component.html'
})
export class PortalDocumentInfoComponent implements OnInit {

  // 当前数据
  fs: any;
  sideDic: any = null;
  columnDic: any = null;
  subjectDic: any = null;
  modeDic: any = null;
  // 附件
  ffs: any = null;
  // 字典
  languageDic: any = null;
  licenseDic: any = null;
  systemDic: any = null;
  workDic: any = null;

  initTool = {
    selector: 'textarea',
    height: '240px',
    fontsize_formats: '10px 12px 14px 16px 18px 24px 36px',
    plugins: [
      'preview advlist autolink link image lists textcolor hr table emoticons code colorpicker codesample',
    ],
    toolbar: 'code insertfile undo redo | fontselect fontsizeselect | bold italic underline strikethrough' +
      ' | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link unlink image' +
      ' | preview media fullpage | forecolor backcolor codesample emoticons',
    language: 'zh_CN',
    language_url: 'assets/tinymce/langs/zh_CN.js',
    image_caption: true,
    branding: false
  };

  constructor(
    public route: ActivatedRoute,
    private modalService: NzModalService) {
    // 注册表单
    this.fs = new form.FormSimpleService('cms_document', {
      orderObject: { publishDate: 0 },
      keyField: 'id',
      randomFields: ['id']
    });

    this.sideDic = new form.DictionaryService('cms_site');
    this.sideDic.loadAll();
    this.columnDic = new form.DictionaryService('cms_column');
    this.columnDic.loadAll();
    this.subjectDic = new form.DictionaryService('cms_subject');
    this.subjectDic.loadAll();
    this.modeDic = new form.DictionaryService('cms_columnmode');
    this.modeDic.loadAll();

    this.languageDic = new form.DictionaryService('cms_document_language');
    this.licenseDic = new form.DictionaryService('cms_document_license');
    this.systemDic = new form.DictionaryService('cms_document_system');
    this.workDic = new form.DictionaryService('cms_nature_work');

    // 文件服务
    this.ffs = new form.FileService();
  }

  ngOnInit() {
    this.initData();

    // 浏览器关闭监听
    window.onbeforeunload = () => {
      // tslint:disable-next-line: deprecation
      const e: any = window.event;
      e.returnValue = ('请确认已保存，是否离开？');
      return e;
    };
  }

  initData() {
    let id = '-1';
    this.route.queryParams.subscribe((res) => {
      if (res.id) {
        id = res.id;
      }
    });
    this.fs.editRowByKey(id);
  }

  save() {
    this.fs.save(this.fs.er, (data) => {
      this.modalService.success({
        nzTitle: '系统提示',
        nzContent: '保存成功！'
      });
      this.fs.er = data;
    }, (error) => {
      this.modalService.error({
        nzTitle: '系统提示',
        nzContent: '保存失败！'
      });
    });
  }

  // 通过
  pass() {
    this.fs.er.state = '2';
    this.save();
  }

  // 不通过
  noPass() {
    this.fs.er.state = '3';
    this.save();
  }

  close() {
    window.close();
  }

  // 站点下拉选择后
  treeClick(e) {
    this.fs.queryObject.subject = null;
    this.fs.queryObject.column = null;
    this.columnDic.filter((item) => {
      if (item.other.siteId === e) {
        return true;
      } else {
        return false;
      }
    });
    this.subjectDic.filter((item) => {
      if (item.other.siteId === e) {
        return true;
      } else {
        return false;
      }
    });
  }
}
